#Michele Gregori 2009 miclegr@gmail.com

import maya.cmds as cmds
import maya.mel as mel

def createCmd(a):
    mel.eval("performSkinCluster 1")
    applyButton=mel.eval("getOptionBoxApplyBtn()")
    try:doButton=applyButton[:-2]+str(int(applyButton[-2:])-1)
    except ValueError:doButton=applyButton[:-1]+str(int(applyButton[-1])-1)
    oldCmd=cmds.button(applyButton,q=True,c=True);
    def parserA(a):
        sel=cmds.ls(sl=True);
        mel.eval(oldCmd)
        for x in sel:
            sctodq(x)
    def parserD(a):
        sel=cmds.ls(sl=True);
        mel.eval(oldCmd)
        for x in sel:
            sctodq(x)
        mel.eval("hideOptionBox")
    cmds.button(applyButton,e=True,c=parserA)
    cmds.button(doButton,e=True,c=parserD,l="dqDeformer")
    mel.eval("setOptionBoxTitle(\"dqDeformer Options\")")
    mel.eval("setOptionBoxHelpTag(\"dqDeformer\")")
    
def sctodq(a):
    print(a)
    if(a==""):a=cmds.ls(sl=True)[0]
    shape=cmds.listRelatives(a,shapes=True)
    if(shape==None):return 
    skinCl=cmds.listConnections(shape[0],t="skinCluster")[0]
    if(skinCl!=""):
        cmds.select(skinCl,r=True)
        dqNode=cmds.dqDeformerCmd()
        cmds.select(dqNode,r=True)
        
def paintDqDeformer(a):
    cmds.ArtPaintSkinWeightsTool()
    name="artAttrSkinContext"
    cmds.makePaintable(activateAll=False)
    cmds.makePaintable("skinCluster","*",activate=True)
    cmds.makePaintable("dqDeformer","paintWeights");    
    if(not(cmds.artAttrSkinPaintCtx(name,exists=True))):
        toRem=cmds.artAttrSkinPaintCtx(name,i1="paintSkinWeights.xpm",whichTool="skinWeights")
        cmds.rememberCtxSettings(toRem)
    cmds.setToolTo(name)
    cmds.toolPropertyWindow()
    
def dqDeformerUI():    
    if(cmds.window("dqDefWin",q=True,ex=True)):
        cmds.deleteUI("dqDefWin")
    cmds.window("dqDefWin",t="dqDeformer1.0",widthHeight=(180,80))
    cmds.columnLayout(columnAttach=("both",5),cw=180)
    cmds.button(l="create new DqDeformer",c=createCmd)
    cmds.button(l="skinCluster to DqDeformer",c=sctodq)
    cmds.button(l="paint DqDeformer",c=paintDqDeformer)
    cmds.showWindow("dqDefWin")
    
dqDeformerUI()

    
    
